<template>
  <div class="app-container">
    <el-form :model="searchForm" :inline="true">
      <el-row>
        <el-col :span="20">
          <el-form-item>
            <el-input v-model="searchForm.username" style="width:200px" placeholder="登录账号" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item>
            <el-input v-model="searchForm.empName" style="width:200px" placeholder="员工姓名" clearable @keyup.enter.native="handleSearch" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="iconfont icon-chaxun1" @click="handleSearch"> 查询</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="4" class="search-right">
          <el-button type="text" icon="el-icon-refresh" @click="handleSearch">刷新</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      border
      :data="sysmonitorData"
      stripe
      highlight-current-row
      :header-cell-style="{background:'#ECF3FC'}"
    >
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column prop="value" label="会话编号" width="160px" align="center" />
      <el-table-column prop="username" label="登录账号" align="center" />
      <el-table-column prop="empName" label="员工姓名" align="center" />
      <el-table-column prop="deptName" label="部门名称" width="130px" align="center" />
      <el-table-column prop="ipAddress" label="IP地址" width="120px" align="center" />
      <el-table-column prop="loginAddressTwo" label="登录地点" width="120px" align="center" />
      <el-table-column prop="browserName" label="浏览器名称" width="120px" align="center" />
      <el-table-column prop="browserVersion" label="浏览器版本" width="120px" align="center" />
      <el-table-column prop="systemName" label="操作系统" width="150px" align="center" />
      <el-table-column prop="createTime" label="登录时间" width="160px" align="center" />
      <el-table-column label="操作" fixed="right" width="120px" align="center">
        <template slot-scope="{row}">
          <el-button type="text" @click="updateQuitOnline(row.userId)">强退</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="searchForm.pageNumber"
        :limit.sync="searchForm.pageSize"
        @pagination="handleSearch"/>
    </div>

  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import * as sysmonitorServer from '@/api/sysmonitor'

export default {
  components: { Pagination },
  data() {
    return {
      searchForm: {
        username: '',
        empName: '',
        pageNumber: 1,
        pageSize: 10
      },
      total: 0,
      sysmonitorData: [],
      address: '',

    }
  },

  created() {
    this.handleSearch()
  },

  methods: {
    handleSearch() {
      sysmonitorServer.findSysUserOnline(this.searchForm).then(res => {
        if (res.code === 200 && res.result) {
          this.sysmonitorData = res.result.records || []
          this.sysmonitorData.map((item) => {
            // fetch(`https://api.vvhan.com/api/getIpInfo?ip=${item.ipAddress}`).then((res) => res.json().then((data) => {
            //     if (data.success === true){
            //       if (data.info.country === '局域网' || data.info.country === '本机地址'){
            //         this.address = '内网IP'
            //       }else {
            //         this.address = data.info.prov + '省 ' + data.info.city
            //       }
            //       this.$set(item, 'loginAddressTwo', this.address)
            //     }
            //   })


            //通过ip地址获取所在地
            fetch(`https://api.vore.top/api/IPdata?ip=${item.ipAddress}`).then((res) => res.json().then((data) => {
              if (data.msg === 'SUCCESS'){
                if (data.ipdata.info1 === '保留IP'){
                  this.address = '内网IP'
                }else {
                  this.address = data.ipdata.info1 + ' ' + data.ipdata.info2
                }
                this.$set(item, 'loginAddressTwo', this.address)
              }
              })
            )


          })
          this.total = res.result.total
        }
      })
    },

    // 强退
    updateQuitOnline(userId) {
      this.$confirm("确定强退吗?", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = { userId: userId };
        sysmonitorServer.updateQuitOnline(data).then(result => {
          if (result.code === 200) {
            this.$notify({
              title: '成功',
              message: result.message,
              type: 'success'
            })
            this.handleSearch()
          } else {
            this.$notify({
              title: '失败',
              message: result.message,
              type: 'error'
            })
          }
        })
      }).catch(() => {})
    },

  }
}
</script>

<style scoped lang="scss" type="text/scss">
.search_div {
  >div {
    &:nth-child(1){
      margin-right: 10px;
    }
    display: inline-block;
  }
}
.search_input {
  width: 200px;
  margin-bottom: 5px;
}
.search-right {
  text-align: right;
  padding-right: 25px;
}
.item {
  font-size: 16px;
}
::v-deep .el-card .el-card__header {
  padding: 5px 20px;
  background: #F3F2F2;
}
.apostrophe{
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.table2  {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
}
/deep/ .vue-treeselect--disabled .vue-treeselect__single-value{
  color: #909399
}
</style>
